package com.google.code.mongoDao;

import java.net.UnknownHostException;

import com.google.inject.AbstractModule;
import com.google.inject.Provides;
import com.google.inject.Singleton;
import com.mongodb.DB;
import com.mongodb.Mongo;
import com.mongodb.MongoException;
import com.mongodb.WriteConcern;

/**
 * DI config
 * 
 * @author MWaschkowski
 *
 */
public class GuiceModuleMongoPersist extends AbstractModule{

  //default database name
  private String dbName = "mongo";
    
	@Override
	protected void configure() {
	}

	@Provides
	@Singleton
	DB provideMongoDb(){
		return provideMongo().getDB(dbName);
	}
	
	@Provides
	@Singleton
	Mongo provideMongo(){
    // connect to the local database server
    Mongo m;
		try {
			m = new Mongo(  );
			m.setWriteConcern(WriteConcern.SAFE);
		} catch (UnknownHostException e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		} catch (MongoException e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}
    return  m;
	}

  public String getDbName() {
    return dbName;
  }

  public void setDbName(String dbName) {
    this.dbName = dbName;
  }

}
